package com.elevator;

import java.awt.Color;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JButton;
import javax.swing.JPanel;

import data.setting;

public class elevatorViews extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public elevatorCtrl mElevatorCtrl = null;
	private Timer mTimer = null;
	public layerUI []mLayerUIs = new layerUI[setting.sLayerCount];
	public liftUI mLiftUI= new liftUI();
	private Color mColor = null;
	private boolean mbOpened = false;
	private int mPersionCount = 0;
	private boolean mAutoGround = false;
	public doorUIEventListener mDoorUIEventListener = null;
	public long mTimeLater = 1000;
	public long mTimeTick =500;
	public elevatorViews()
	{
		this(true);
	}
	public elevatorViews(boolean bLiftVisible)
	{
		mAutoGround = !bLiftVisible;
		mElevatorCtrl = new elevatorCtrl();
		liftUI liftUI= mLiftUI;
		liftUI.mListener = new liftUIEventListener() {
			
			@Override
			public void liftUIEventProcess(int nEvent, Object object) {
				// TODO Auto-generated method stub
				JButton btn = (JButton)object;
				int nIndex  =0;
				if (!btn.getText().equals("G")) {
					nIndex = nEvent;
				}
				mElevatorCtrl.mElevator.mBtns[nIndex] = 1;
				btn.setBackground(Color.red);
			}
		};
		
		this.setLayout(null);
		this.add(liftUI);
		liftUI.setLocation(130, 0+100*(setting.sLayerCount-1));
		layerUI [] layerUIs=mLayerUIs;
		int x = 10 ;
		int y = 10;
		for (int i = 0; i < setting.sLayerCount; i++)
		{
			layerUIs[i] = new layerUI();
			layerUIs[i].mLayer = i;
			layerUIs[i].mDoorUI.mListener = new doorUIEventListener() {
				
				@Override
				public void doorUIEvent(doorUIStatus nStatus) {
					// TODO Auto-generated method stub
					synchronized(elevatorViews.this)
					{
						if (mDoorUIEventListener != null) {
							mDoorUIEventListener.doorUIEvent(nStatus);
						}
						
						if (nStatus == doorUIStatus.doorUIClosed)
						{
							mbOpened = false;
							startTimer();	
						}
						else if ( nStatus == doorUIStatus.doorUIOpened)
						{

						}

					}

				}
			};
			this.setSize(300, setting.sLayerCount<=2?280:setting.sLayerCount*110);
			layerUIs[i].mEventListenerEvent = new layerUIEventListener() {
				
				@Override
				public void ActionEvent(int nActionType, Object srcObjec) {
					// TODO Auto-generated method stub
					layerUI layerUI= (layerUI)srcObjec;
					if (nActionType == 1)
					{
						mElevatorCtrl.mLayers.get(layerUI.mLayer).mUpBtnStatus = 1;
						layerUI.mUpBtn.setBackground(Color.red);
					}
					else if(nActionType == -1){
						mElevatorCtrl.mLayers.get(layerUI.mLayer).mDownBtnStatus = 1;
						layerUI.mDownBtn.setBackground(Color.red);
					}

				}
			};
			this.add(layerUIs[i]);
			layerUIs[i].setLocation(x, y+((setting.sLayerCount -1 )-i) *layerUIs[i].getHeight());
			if (i>0)
			{
				layerUIs[i].mCurrentLayer.setText(String.valueOf(i));
			}
			else 
			{
				layerUIs[i].mCurrentLayer.setText("G");
			}
			
		
			if (i == 0 ) {
				layerUIs[i].mDownBtn.setVisible(false);
			}else if(i == setting.sLayerCount - 1){
				layerUIs[i].mUpBtn.setVisible(false);
			}
			
		}
		if (mLayerUIs.length > 0) {
			mColor = mLayerUIs[0].mDownBtn.getBackground();
		}
		
		if(!bLiftVisible)
		{
			mLiftUI.setVisible(false);
		}
		
		//
	}
	public void setPersonNum(int num) {
		mPersionCount = num;
		for (int i = 0; i < setting.sLayerCount; i++)
		{
			mLayerUIs[i].mDoorUI.setPersonNum(num);
		}
		mLiftUI.mDoorUI.setPersonNum(num);
	}
	public int getPersonNum()
	{
		return mPersionCount;
	}
	public void startTimer()
	{
		mTimer = new Timer();
		mTimer.schedule(new TimerTask() {
			
			@Override
			public void run() {
				// TODO Auto-generated method stub
				synchronized(elevatorViews.this)
				{
					//stop
					if (mElevatorCtrl.mElevator.mMovingStatus == 0)
					{
						
						testMoveWhenStop();
					}
					else if (mElevatorCtrl.mElevator.mMovingStatus ==1)
					{
						testMoveWhenUp();
					}
					else if (mElevatorCtrl.mElevator.mMovingStatus == -1)
					{
						testMoveWhenDown();
					}
					
					elevatorViews.this.setUpOrDownLabel(mElevatorCtrl.mElevator.mMovingStatus);
					mLiftUI.setLocation(130, 0+100*((setting.sLayerCount - mElevatorCtrl.mElevator.mCurrentLayer)-1));
				}

			}
			
		}, mTimeLater, mTimeTick);
	}
	
	public void setUpOrDownLabel(int nType) 
	{
		for (int i = 0; i < mLayerUIs.length; i++) {
			if (nType == 0) {
				mLayerUIs[i].mDownLabel.setForeground(Color.black);
				mLayerUIs[i].mUpLabel.setForeground(Color.black);
				if (mElevatorCtrl.mElevator.mCurrentLayer >0)
				{
					mLayerUIs[i].mCurrentLiftInLayer.setText(""+(mElevatorCtrl.mElevator.mCurrentLayer));
				}
				else 
				{
					mLayerUIs[i].mCurrentLiftInLayer.setText("G");
				}
			}
			else if(nType == 1)
			{
				mLayerUIs[i].mDownLabel.setForeground(Color.black);
				mLayerUIs[i].mUpLabel.setForeground(Color.red);
				if (mElevatorCtrl.mElevator.mCurrentLayer >0)
				{
					mLayerUIs[i].mCurrentLiftInLayer.setText(""+(mElevatorCtrl.mElevator.mCurrentLayer));
				}
				else 
				{
					mLayerUIs[i].mCurrentLiftInLayer.setText("G");
				}
			}
			else if (nType == -1)
			{
				mLayerUIs[i].mDownLabel.setForeground(Color.red);
				mLayerUIs[i].mUpLabel.setForeground(Color.black);	
				if (mElevatorCtrl.mElevator.mCurrentLayer >0)
				{
					mLayerUIs[i].mCurrentLiftInLayer.setText(""+(mElevatorCtrl.mElevator.mCurrentLayer));
				}
				else 
				{
					mLayerUIs[i].mCurrentLiftInLayer.setText("G");
				}
				
			}
		}
		if (mElevatorCtrl.mElevator.mCurrentLayer>0)
		{
			mLiftUI.mLabel.setText(""+(mElevatorCtrl.mElevator.mCurrentLayer));
		}
		else 
		{
			mLiftUI.mLabel.setText("G");
		}
		
	}
	private void testMoveWhenStop()
	{
		int nUpOfCurIndex = 0;
		int nDownOfCurIndex = 0;
		int nCurIndex = mElevatorCtrl.mElevator.mCurrentLayer;
		for (int i = 0; i < setting.sLayerCount; i++) 
		{
			if (mElevatorCtrl.mElevator.mBtns[i] == 1
					||mElevatorCtrl.mLayers.get(i).mDownBtnStatus == 1
					||mElevatorCtrl.mLayers.get(i).mUpBtnStatus == 1) 
			{
				if (nCurIndex > i) {
					nDownOfCurIndex = 1;
				}
				else if(nCurIndex < i)
				{
					nUpOfCurIndex = 1;
				}
				else 
				{
					if (mElevatorCtrl.mLayers.get(i).mDownBtnStatus == 1)
					{
						mElevatorCtrl.mLayers.get(i).mDownBtnStatus =0;
						mLayerUIs[nCurIndex].mDownBtn.setBackground(mColor);
					}
					else if(mElevatorCtrl.mLayers.get(i).mUpBtnStatus == 1)
					{
						mElevatorCtrl.mLayers.get(i).mUpBtnStatus =0;
						mLayerUIs[nCurIndex].mUpBtn.setBackground(mColor);
					}
					else if(mElevatorCtrl.mElevator.mBtns[i] == 1)
					{
						mElevatorCtrl.mElevator.mBtns[i] = 0;
						mLiftUI.mBtns[i].setBackground(mColor);
					}
					startOpenDoor();
					break;
				}
			}
		}
		if (this.mAutoGround && nUpOfCurIndex ==0 && nDownOfCurIndex ==0
                        && nCurIndex ==0 && this.mDoorUIEventListener !=null)
                {
                    this.mDoorUIEventListener.doorUIEvent(doorUIStatus.doorUIReset);
                }
		if (nUpOfCurIndex == 1) {
			mElevatorCtrl.mElevator.mMovingStatus = 1;
		}
		else if(nDownOfCurIndex == 1)
		{
			mElevatorCtrl.mElevator.mMovingStatus = -1;
		}
		else
		{
			
			if (mAutoGround && nCurIndex != 0) {
				mElevatorCtrl.mElevator.mMovingStatus = -1;
			}
			else
			{
				mElevatorCtrl.mElevator.mMovingStatus = 0;
			}
		}
	}

	private  void testMoveWhenUp()
	{
		int nUpOfCurIndex = 0;
		int nDownOfCurIndex = 0;
		if (mElevatorCtrl.mElevator.mCurrentLayer < setting.sLayerCount -1) {
			mElevatorCtrl.mElevator.mCurrentLayer++;
		}
		int nCurIndex = mElevatorCtrl.mElevator.mCurrentLayer;
		for (int i = 0; i < setting.sLayerCount; i++) 
		{
			if (mElevatorCtrl.mElevator.mBtns[i] == 1) 
			{
				if (nCurIndex > i) {
					nDownOfCurIndex = 1;
				}
				else if(nCurIndex < i)
				{
					nUpOfCurIndex = 1;
				}
				else 
				{
					mElevatorCtrl.mElevator.mBtns[i] = 0;
					mLiftUI.mBtns[i].setBackground(mColor);
					startOpenDoor();
				}

			}
			
			if (mElevatorCtrl.mLayers.get(i).mUpBtnStatus == 1
					||mElevatorCtrl.mLayers.get(i).mDownBtnStatus == 1) {
				if (nCurIndex > i) {
					nDownOfCurIndex = 1;
				}
				else if(nCurIndex < i)
				{
					nUpOfCurIndex = 1;
				}
			}
		}
		if (mElevatorCtrl.mLayers.get(nCurIndex).mUpBtnStatus == 0
				&&nUpOfCurIndex == 0 && mElevatorCtrl.mLayers.get(nCurIndex).mDownBtnStatus == 1)
		{
			mElevatorCtrl.mLayers.get(nCurIndex).mDownBtnStatus = 0;
			mLayerUIs[nCurIndex].mDownBtn.setBackground(mColor);
			startOpenDoor();
		}
		else if (mElevatorCtrl.mLayers.get(nCurIndex).mUpBtnStatus == 1)
		{
			mElevatorCtrl.mLayers.get(nCurIndex).mUpBtnStatus = 0;
			mLayerUIs[nCurIndex].mUpBtn.setBackground(mColor);
			startOpenDoor();
		}
		
		if (nUpOfCurIndex == 0 && nDownOfCurIndex == 1) {
			mElevatorCtrl.mElevator.mMovingStatus = -1;
		}
		else if (nUpOfCurIndex == 0 && nDownOfCurIndex == 0)
		{
			mElevatorCtrl.mElevator.mMovingStatus = 0;
		}
		
	}
	private void testMoveWhenDown()
	{
		int nUpOfCurIndex = 0;
		int nDownOfCurIndex = 0;
		if (mElevatorCtrl.mElevator.mCurrentLayer >0) {
			mElevatorCtrl.mElevator.mCurrentLayer--;
		}
		int nCurIndex = mElevatorCtrl.mElevator.mCurrentLayer;
		for (int i = 0; i < setting.sLayerCount; i++) 
		{
			if (mElevatorCtrl.mElevator.mBtns[i] == 1) 
			{
				if (nCurIndex > i) {
					nDownOfCurIndex = 1;
				}
				else if(nCurIndex < i)
				{
					nUpOfCurIndex = 1;
				}
				else 
				{
					mElevatorCtrl.mElevator.mBtns[i] = 0;
					mLiftUI.mBtns[i].setBackground(mColor);
					startOpenDoor();
				}

			}
			if (mElevatorCtrl.mLayers.get(i).mUpBtnStatus == 1
					||mElevatorCtrl.mLayers.get(i).mDownBtnStatus == 1) {
				if (nCurIndex > i) {
					nDownOfCurIndex = 1;
				}
				else if(nCurIndex < i)
				{
					nUpOfCurIndex = 1;
				}
			}
		}
		if (mElevatorCtrl.mLayers.get(nCurIndex).mDownBtnStatus == 0
				&&nDownOfCurIndex == 0 &&mElevatorCtrl.mLayers.get(nCurIndex).mUpBtnStatus == 1)
		{
			mElevatorCtrl.mLayers.get(nCurIndex).mUpBtnStatus = 0;
			mLayerUIs[nCurIndex].mUpBtn.setBackground(mColor);
			startOpenDoor();
		}
		
		else if ( mElevatorCtrl.mLayers.get(nCurIndex).mDownBtnStatus == 1)
		{
			mElevatorCtrl.mLayers.get(nCurIndex).mDownBtnStatus = 0;
			mLayerUIs[nCurIndex].mDownBtn.setBackground(mColor);
			startOpenDoor();
		}
		
		if (nUpOfCurIndex == 1 && nDownOfCurIndex == 0) {
			mElevatorCtrl.mElevator.mMovingStatus = 1;
		}
		else if (nUpOfCurIndex == 0 && nDownOfCurIndex == 0)
		{
			mElevatorCtrl.mElevator.mMovingStatus = 0;
		}
	}
	
	private void startOpenDoor() {
		// TODO Auto-generated method stub
		if (!mbOpened) {
			mbOpened = true;
			mTimer.cancel();
			
			mLiftUI.mDoorUI.startOpenDoor();
			mLayerUIs[mElevatorCtrl.mElevator.mCurrentLayer].mDoorUI.startOpenDoor();
		}
		
	}
	public void StopTimer()
	{
		if (mTimer != null) {
			mTimer.cancel();
		}
	}
}
